package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwSceincEntertain;
import com.xwdeshj.entity.XwSceincEntertainment;
import com.xwdeshj.repository.XwSceincEntertainmentRepository;
import com.xwdeshj.service.XwSceincEntertainmentService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2019/1/7.
 * 景区——景区娱乐项目
 */
@Slf4j
@Service("xwSceincEntertainmentService")
public class XwSceincEntertainmentServiceImpl implements XwSceincEntertainmentService {

    @Autowired
    private XwSceincEntertainmentRepository xwSceincEntertainmentRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_XwSceincEntertain dal_xwSceincEntertain = new Dal_XwSceincEntertain();

    /**
     * 获取景区店铺 娱乐项目信息
     */
    @Override
    public JSONObject getEntertainlist(Integer pageindex, Integer pagesize, String starttime, String endtime,
                                       String sortfild, String sortway, String searchval, String searchkey,
                                       String sceinc_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwSceincEntertain.getEntertainlist(pageindex, pagesize, starttime, endtime, sortfild, sortway,
                searchval, searchkey, sceinc_encryptId, jdbcTemplate);

        System.out.println("getEntertainlist:" + result.toString());
        return result;
    }

    /**
     * 景区——新建娱乐项目
     *
     * @param business_encryptId 商家加密id
     * @param scenic_encryptId   店铺加密id
     * @param enterid            娱乐项目id，新建：0
     * @param enterName          娱乐项目名
     * @param enterIntr          娱乐项目简介
     * @param parmJson           娱乐项目展示图，id：娱乐项目id，imglink：图片链接
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject updateEntertain(String business_encryptId, String scenic_encryptId, String enterid,
                                      String enterName, String enterIntr, String parmJson,
                                      HttpServletRequest request) {
        JSONObject result = new JSONObject();

        String uid = helper.ReadCookieMap(request, "uid").toString();//读取当前操作人用户id
        String u_encryptid = helper.ReadCookieMap(request, "uid_encryptid").toString();//读取当前操作人用户加密id
        if ((!"".equals(uid) && uid != null) && (!"".equals(u_encryptid) && u_encryptid != null)) {

            if (!business_encryptId.equals("") && scenic_encryptId != "") {

                String id = "";
                String imglink = "";
                JSONArray jsonArray = new JSONArray();
                jsonArray = JSON.parseArray(parmJson);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    id = object.getString("id");
                    if (imglink.equals("")) {
                        imglink = object.getString("imglink");
                    } else {
                        if(!object.getString("imglink").equals("")){
                            imglink += "," + object.getString("imglink");
                        }
                    }
                }
                if (enterid.equals("0") || enterid.equals("")) {
                    //新建
                    XwSceincEntertainment xwSceincEntertainment = new XwSceincEntertainment();
                    xwSceincEntertainment.setBusinessEncryptid(business_encryptId);
                    xwSceincEntertainment.setScenicEncryptid(scenic_encryptId);
                    xwSceincEntertainment.setScenicEnterName(enterName);
                    xwSceincEntertainment.setScenicEnterDesc(enterIntr);
                    xwSceincEntertainment.setScenicEnterImg(imglink);
                    xwSceincEntertainment.setScenicEnterCreatetime(new Date());
                    xwSceincEntertainment.setScenicEnterUpdatetime(new Date());
                    try {
                        xwSceincEntertainmentRepository.save(xwSceincEntertainment);
                        result.put("status", 0);
                        result.put("msg", "ok");
                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status", 40002);
                        result.put("msg", e);
                    }

                } else {
                    //修改
                    XwSceincEntertainment xwSceincEntertainment = new XwSceincEntertainment();
                    xwSceincEntertainment.setScenicEnterId(Integer.parseInt(enterid));
                    boolean temp = xwSceincEntertainmentRepository.exists(Example.of(xwSceincEntertainment));
                    if (temp){
                        XwSceincEntertainment sceincEntertainment = xwSceincEntertainmentRepository.findByScenicEnterId(Integer.parseInt(enterid));
                        sceincEntertainment.setScenicEnterName(enterName);
                        sceincEntertainment.setScenicEnterDesc(enterIntr);
                        sceincEntertainment.setScenicEnterImg(imglink);
                        sceincEntertainment.setScenicEnterUpdatetime(new Date());
                        try {
                            xwSceincEntertainmentRepository.save(sceincEntertainment);
                            result.put("status", 0);
                            result.put("msg", "ok");
                        }catch (Exception e){
                            e.printStackTrace();
                            result.put("status", 40002);
                            result.put("msg", e);
                        }

                    }
                }

            }else {
                result.put("status", 40002);
                result.put("msg", "business_encryptId,scenic_encryptId不能为空！");
            }

        } else {
            result.put("status", 40001);
            result.put("msg", "uid不能为空,请先登录！");
        }
        System.out.println("updateEntertain:" + result.toString());
        return result;
    }

    /**
     * 根据 店铺id，娱乐项目id 获取 一条娱乐项目信息
     */
    @Override
    public JSONObject getEntertainOne(String scenic_encryptId, String enterid, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwSceincEntertain.getEntertainOne(scenic_encryptId,enterid,jdbcTemplate);

        System.out.println("getEntertainOne:" + result.toString());
        return result;
    }


    /**
     * 根据娱乐项目id，删除对应的信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delsceinEnter(String enterid, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!enterid.equals("")){
                if(enterid.indexOf(",") == -1){
                    try {
                        //单个删除
                        int flag = xwSceincEntertainmentRepository.deleteByScenicEnterId(Integer.parseInt(enterid));
                        if(flag == 1){
                            result.put("status",0);
                            result.put("msg","ok");
                        }else {
                            result.put("status",40002);
                            result.put("msg","删除失败！");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }

                }else {
                    //批量删除
                    int flag = 0;
                    String[] tids = enterid.split(",");
                    for (int i = 0; i < tids.length; i++) {
                        flag = xwSceincEntertainmentRepository.deleteByScenicEnterId(Integer.parseInt(tids[i]));
                    }
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",40002);
                        result.put("msg","接口出现错误！");
                    }
                }
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("delsceinEnter:"+result.toString());
        return result;
    }


    public Dal_XwSceincEntertain getDal_xwSceincEntertain() {
        return dal_xwSceincEntertain;
    }

    public void setDal_xwSceincEntertain(Dal_XwSceincEntertain dal_xwSceincEntertain) {
        this.dal_xwSceincEntertain = dal_xwSceincEntertain;
    }
}
